package com.aso114.project.util;

import android.content.SharedPreferences;

import com.aso114.project.base.BaseApplication;

import java.io.IOException;
import java.util.HashSet;

import okhttp3.Interceptor;
import retrofit2.Response;

public class ReceivedCookiesInterceptor implements Interceptor {
    @Override public okhttp3.Response intercept(Chain chain) throws IOException {

        okhttp3.Response originalResponse = chain.proceed(chain.request());

        if (!originalResponse.headers("Set-Cookie").isEmpty())
        { HashSet<String> cookies = new HashSet<>();
        for (String header : originalResponse.headers("Set-Cookie"))
        { cookies.add(header); }
        SharedPreferences.Editor config = BaseApplication.getInstance().getSharedPreferences("config", BaseApplication.getInstance().MODE_PRIVATE) .edit();

        config.putStringSet("cookie", cookies); config.commit(); }

        return originalResponse; } }


